'''
t分布
概率密度函数图像
'''

import numpy as np
from matplotlib import pyplot as plt
from pylab import mpl
plt.style.use('seaborn-darkgrid')
mpl.rcParams['font.sans-serif'] = ['SimHei']  # 显示中文字体
plt.rcParams['axes.unicode_minus'] = False  # 显示负号


def gamma_function(n):
    cal = 1
    for i in range(2, n):
        cal *= i
    return cal


def student_t(x, freedom, n):

    # divide [x.min(), x.max()] by n
    x = np.linspace(x.min(), x.max(), n)

    c = gamma_function((freedom + 1) // 2) \
        / np.sqrt(freedom * np.pi) * gamma_function(freedom // 2)
    y = c * (1 + x**2 / freedom)**(-((freedom + 1) / 2))

    return x, y, np.mean(y), np.std(y)


for freedom in [1, 2, 5]:

    x = np.arange(-10, 10)  # define range of x
    x, y, _, _ = student_t(x, freedom=freedom, n=10000)
    plt.plot(x, y, label=r'$v=%d$' % (freedom))

plt.legend(loc=1)
plt.xlabel('x', fontsize=12)
plt.ylabel('f(x)', fontsize=12)
plt.title("t分布——不同自由度的概率密度函数")
plt.show()
